package com.ddyx.service.impl.user;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.user.UCompany;
import com.ddyx.mapper.user.UCompanyMapper;
import com.ddyx.service.user.UCompanyService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Service
@Slf4j
public class UCompanyServiceImpl implements UCompanyService{
	
	@Resource
	UCompanyMapper companyMapper;

	@Override
	public ResponseForm getCompanyList() {
		ResponseForm result = new ResponseForm();
		try {
			List<UCompany> companyList = companyMapper.selectAll();
			result.setData(companyList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getCompanyListPage(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map paramMap = (Map) param.getData();
			com.ddyx.util.Page.parsePage(paramMap);
			Example example = new Example(UCompany.class);
			example.setOrderByClause("join_time DESC");
			Page<Object> page = PageHelper.startPage((Integer)paramMap.get("page"), (Integer)paramMap.get("size"));
			List<UCompany> companyList = companyMapper.selectAll();
			result.setData(companyList);
			result.setTotal((int) page.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm addCompany(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			UCompany bean = new UCompany();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			MyBeanUtils.populate(bean, paramMap);
			companyMapper.insertSelective(bean);
			result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	@Transactional
	@Override
	public ResponseForm updateCompany(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			UCompany bean = new UCompany();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			MyBeanUtils.populate(bean, paramMap);
			companyMapper.updateByPrimaryKeySelective(bean);
			//result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getCompanyById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			UCompany bean = new UCompany();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			MyBeanUtils.populate(bean, paramMap);
			bean =companyMapper.selectByPrimaryKey(bean.getCompanyId());
			result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
}


